import logging
import os
import time
from logging.handlers import RotatingFileHandler
from queue import Queue

import xlwings as xw
from apscheduler.schedulers.background import BlockingScheduler

from config import SRC_CTX_MAIN_DICT
from process import PROCESS
from storage import STORAGE
from url import get_countries


# REF: https://blog.csdn.net/smilehappiness/article/details/117265531
# REF: https://blog.csdn.net/weixin_39844901/article/details/110788098
# REF: https://blog.csdn.net/Rebacca122222/article/details/124440089


def task():
    desktop_path = os.path.join(os.path.expanduser('~'), "Desktop\\") + \
                   f"{time.strftime('%Y%m%d-%H%M', time.localtime())}报表.xlsx"

    logging.info(f"---------- {time.strftime('%Y%m%d-%H%M', time.localtime())} 准备开始抓取 ----------")
    q = Queue()
    city_dict = get_countries()
    for name, url in city_dict.items():
        q.put({
            'countries_name': name,
            'countries_url': url
        })
    for i in range(1, 9):
        thread = PROCESS(i, q)
        thread.daemon = True
        thread.start()
    q.join()

    book = xw.Book()
    s = STORAGE(book)
    s.process(SRC_CTX_MAIN_DICT)
    book.save(desktop_path)
    book.close()
    logging.info("---------- 抓取完备 ----------")


if __name__ == '__main__':
    app = logging.getLogger()
    app.setLevel(logging.INFO)
    streamHandler = logging.StreamHandler()
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
    streamHandler.setFormatter(formatter)
    rotatingHandler = logging.handlers.RotatingFileHandler('./run.log', maxBytes=5 * 1024 * 1024, backupCount=5)
    app.addHandler(streamHandler)
    app.addHandler(rotatingHandler)

    task()
    scheduler = BlockingScheduler()
    scheduler.add_job(task, 'interval', days=1)
    scheduler.start()
